package com.gray.blog.controller.sys;

import cn.hutool.core.util.StrUtil;
import com.gray.blog.arg.LoginArg;
import com.gray.blog.common.annotation.IpCheckTrigger;
import com.gray.blog.common.controller.BaseController;
import com.gray.blog.common.enums.ErrorCodeEnum;
import com.gray.blog.common.result.ModelResult;
import com.gray.blog.common.util.IpAddressUtil;
import com.gray.blog.common.util.JWTUtil;
import com.gray.blog.common.util.thread.SysUserThreadPoolUtils;
import com.gray.blog.dao.sys.SysUserDao;
import com.gray.blog.entity.sys.SysUserEntity;
import com.gray.blog.result.LoginResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * Created  By zhoux 2019/01/09
 **/
@Slf4j
@RestController
@RequestMapping(value = "/open")
@Api(value = "登录接口", description = "登录相关接口")
@CrossOrigin
public class LoginController extends BaseController {

    @Autowired
    private SysUserDao sysUserDao;

    @RequestMapping(value = "/verifyLogin/", method = RequestMethod.GET)
    @IpCheckTrigger
    @ApiOperation(value = "shiro登录过度接口")
    public ModelResult verifyLogin() {
        return ModelResult.newError(ErrorCodeEnum.USER_NOT_LOGIN);
    }

    @RequestMapping(value = "/notRole/", method = RequestMethod.GET)
    @IpCheckTrigger
    @ApiOperation(value = "无权限接口")
    public ModelResult notRole() {
        return ModelResult.newError(ErrorCodeEnum.USER_NO_PERMISSION);
    }

    @RequestMapping(value = "/login/", method = RequestMethod.POST)
    @IpCheckTrigger
    @ApiOperation(value = "登录接口")
    public ModelResult<LoginResult> login(@RequestBody @Valid LoginArg arg) {
        LoginResult result = new LoginResult();
        SysUserEntity sysUserEntity = sysUserDao.getInfoByLoginName(arg.getUserName());
        if (sysUserEntity == null || StrUtil.isBlank(sysUserEntity.getPassword())) {
            return ModelResult.newError(ErrorCodeEnum.LOGIN_FAIL);
        } else if (!StrUtil.equals(sysUserEntity.getPassword(), arg.getPassword())) {
            return ModelResult.newError(ErrorCodeEnum.LOGIN_FAIL);
        } else {
            result.setUserName(arg.getUserName());
            result.setToken(JWTUtil.createToken(arg.getUserName()));
            result.setId(sysUserEntity.getId());
            // 异步更新登录时间
            SysUserThreadPoolUtils.execute(() -> {
                sysUserDao.updateUserLoginInfo(arg.getLoginIp(), arg.getUserName());
            });
            return ModelResult.newSuccess(result);
        }
    }

    @RequestMapping(value = "/logout/", method = RequestMethod.GET)
    @IpCheckTrigger
    @ApiOperation(value = "登出/注销")
    public ModelResult logout() {
        Subject subject = SecurityUtils.getSubject();
        //注销
        subject.logout();
        return ModelResult.newSuccess();
    }

    @RequestMapping(value = "/verifyToken/", method = RequestMethod.POST)
    @IpCheckTrigger
    @ApiModelProperty(value = "校验token")
    public ModelResult verifyToken(@RequestHeader String token, @RequestHeader String userName) {
        // 校验token
        return JWTUtil.verifyToken(token, userName) ? ModelResult.newError(ErrorCodeEnum.SUCCESS) : ModelResult.newError(ErrorCodeEnum.TOKEN_INVALID);
    }

}
